package health.lm.com.component_base.popup.share;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.view.RxView;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import java.util.concurrent.TimeUnit;

import health.lm.com.component_base.R;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.popup.uplevel.UpLevelBean;
import health.lm.com.component_base.widget.CustomPopWindow;
import health.lm.com.data.App;
import health.lm.com.data.HttpCST;
import health.lm.com.router.ARouterConstant;

/**
 * 结果提示弹窗
 */

public class NoticePopUtil {
    private static NoticePopUtil sharePopUtil;
    private static Context ctx;
    private CustomPopWindow popWindow;
    private String url;
    private String title;
    private String description;
    private IWXAPI api;

    public static NoticePopUtil getInstance() {
        if (sharePopUtil == null)
            sharePopUtil = new NoticePopUtil();
        return sharePopUtil;
    }

    /**
     * 显示popwindow
     */
    @SuppressLint("CheckResult")
    public void showShare(Context context, View mLayoutPopup, UpLevelBean bean) {
        ctx = context;
        //url = TestImgEntity.img1;
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_share_notice, null);
        //图片
        ImageView img = contentView.findViewById(R.id.iv_icon);
        ImageLoaderHelper.getInstance().load(context, App.model.getCoin_url(), img);
        //数量
        TextView tvContent1 = contentView.findViewById(R.id.tv_count);
        tvContent1.setText(bean.getSugar());
        //内容
        TextView tvContent2 = contentView.findViewById(R.id.tv_content);
        tvContent2.setText(bean.getStr());
        Button button = contentView.findViewById(R.id.button);
        ImageView ivAD = contentView.findViewById(R.id.imageView2);
        ImageView ivClose = contentView.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(v -> popWindow.dissmiss());
        /*-------------------------------------数据控制--------------------------------------------*/
        //up_status 是否显示 按钮 0否1是
        button.setVisibility(bean.getUp_status().equals("0") ? View.GONE : View.VISIBLE);
        if (bean.getUp_status().equals("1")) {
            button.setText(bean.getUp_title());
            //跳转外连
            RxView.clicks(button).throttleFirst(500, TimeUnit.MILLISECONDS)
                    .subscribe(o -> ARouter.getInstance().build(ARouterConstant.InviteWebViewActivity)
                            .withString("url", bean.getLink_url()).navigation());
        }
        //ad_status 是否显示广告 0否1是
        ivAD.setVisibility(bean.getAd_status().equals("0") ? View.GONE : View.VISIBLE);
        if (bean.getAd_status().equals("1")) {
            ImageLoaderHelper.getInstance().load(context, bean.getAd_img(), ivAD);
            RxView.clicks(ivAD).throttleFirst(500, TimeUnit.MILLISECONDS)
                    .subscribe(o -> onJump(bean.getType(), bean.getLink_url()));
        }
        /*-----------------------------------------------------------------------------------------*/

        //创建并显示popWindow
        //显示大小
        popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(contentView)
                .enableBackgroundDark(true)
                .setBgDarkAlpha(0.8f)
                .setAnimationStyle(R.style.pop_center_anim)
                //.size(500, 500)//显示大小
                .setOutsideTouchable(false)
                .create()
                .showAtLocation(mLayoutPopup, Gravity.CENTER, 0, 0);
    }

    /**
     * 显示popwindow  糖果加减
     */
    public void show(Context context, View mLayoutPopup, String content, String content2) {
        ctx = context;
        //url = TestImgEntity.img1;
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_notice, null);
        //图片
        ImageView img = contentView.findViewById(R.id.iv_icon);
        ImageLoaderHelper.getInstance().load(context, App.model.getSugar_url(), img);
        //数量
        TextView tvContent1 = contentView.findViewById(R.id.tv_count);
        tvContent1.setText(content);
        //内容
        TextView tvContent2 = contentView.findViewById(R.id.tv_content);
        tvContent2.setText(content2);
        //创建并显示popWindow
        //显示大小
        popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(contentView)
                .enableBackgroundDark(true)
                .setBgDarkAlpha(0.8f)
                .setAnimationStyle(R.style.pop_center_anim)
                .size(500, 500)//显示大小
                .create()
                .showAtLocation(mLayoutPopup, Gravity.CENTER, 0, 0);
    }

    public void onJump(String type, String link) {
        switch (type) {
            case ARouterConstant.JUMP_NOTICE://跳转通知
                ARouter.getInstance().build(ARouterConstant.NoticesDetailActivity)
                        .withString(HttpCST._id, link).navigation();
                break;
            case ARouterConstant.JUMP_LINK://跳转外链
                ARouter.getInstance().build(ARouterConstant.InviteWebViewActivity)
                        .withString("url", link).navigation();
                break;
            case ARouterConstant.JUMP_GOODS://跳转商品 HttpCST._id
                ARouter.getInstance().build(ARouterConstant.ProductDetailActivity)
                        .withString(HttpCST.PRODUCT_ID, link).navigation();
                break;
            //TODO 拍卖预售 link有值就跳转详情、没有值就跳转列表
            case ARouterConstant.JUMP_AUCTION://跳转拍卖
                break;
            case ARouterConstant.JUMP_PRE_SALE://跳转预售
                ARouter.getInstance().build(ARouterConstant.MallPreSaleActivity).navigation();
                break;
        }
    }
}
